package com.woniuxy.aimcpserver.mapper;

import com.woniuxy.aimcpserver.entity.Doctor;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DoctorMapper {

    /**
     * 查询所有医生信息
     * @return 医生列表
     */
    @Select("SELECT " +
            "doctor_id AS doctorId, " +
            "name, " +
            "title, " +
            "department_id AS departmentId, " +
            "specialty, " +
            "is_active AS isActive, " +
            "avatar " +
            "FROM doctors")
    @Results({
            @Result(property = "doctorId", column = "doctor_id", id = true),
            @Result(property = "name", column = "name"),
            @Result(property = "title", column = "title"),
            @Result(property = "departmentId", column = "department_id"),
            @Result(property = "specialty", column = "specialty"),
            @Result(property = "isActive", column = "is_active"),
            @Result(property = "avatar", column = "avatar")
    })
    List<Doctor> findAllDoctors();

    /**
     * 根据ID查询医生信息
     * @param doctorId 医生ID
     * @return 医生信息
     */
    @Select("SELECT * FROM doctor WHERE doctor_id = #{doctorId}")
    @ResultMap("doctorResultMap")
    Doctor findById(@Param("doctorId") Integer doctorId);

    /**
     * 插入医生信息
     * @param doctor 医生实体
     * @return 影响的行数
     */
    @Insert("INSERT INTO doctor(name, title, department_id, specialty, is_active, avatar) " +
            "VALUES(#{name}, #{title}, #{departmentId}, #{specialty}, #{isActive}, #{avatar})")
    @Options(useGeneratedKeys = true, keyProperty = "doctorId")
    int insert(Doctor doctor);

    /**
     * 更新医生信息
     * @param doctor 医生实体
     * @return 影响的行数
     */
    @Update("UPDATE doctor SET " +
            "name = #{name}, " +
            "title = #{title}, " +
            "department_id = #{departmentId}, " +
            "specialty = #{specialty}, " +
            "is_active = #{isActive}, " +
            "avatar = #{avatar} " +
            "WHERE doctor_id = #{doctorId}")
    int update(Doctor doctor);

    /**
     * 根据ID删除医生
     * @param doctorId 医生ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM doctor WHERE doctor_id = #{doctorId}")
    int deleteById(@Param("doctorId") Integer doctorId);


    /**
     * 根据医生姓名模糊查询医生信息
     * @param name 医生姓名或部分姓名
     * @return 匹配的医生列表
     */
    @Select("SELECT " +
            "doctor_id AS doctorId, " +
            "name, " +
            "title, " +
            "department_id AS departmentId, " +
            "specialty, " +
            "is_active AS isActive, " +
            "avatar " +
            "FROM doctor " +
            "WHERE name LIKE CONCAT('%', #{name}, '%')")
    @Results(id = "doctorResultMap", value = {
            @Result(property = "doctorId", column = "doctor_id", id = true),
            @Result(property = "name", column = "name"),
            @Result(property = "title", column = "title"),
            @Result(property = "departmentId", column = "department_id"),
            @Result(property = "specialty", column = "specialty"),
            @Result(property = "isActive", column = "is_active"),
            @Result(property = "avatar", column = "avatar")
    })
    List<Doctor> findDoctorsByName(@Param("name") String name);
}